package cglib;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.CallbackFilter;
import net.sf.cglib.proxy.Dispatcher;
import net.sf.cglib.proxy.Enhancer;

public class TestDispatcher {

	final static Object[] impls = new Object[] { 
	new Foo() {
		public String foo() {
			return "foo1";
		}
	}, 
	new Bar() {
		public String bar() {
			return "bar1";
		}
	} };

	private static Object create(){
		Callback[] callbacks = new Callback[]{
	            new Dispatcher() {
	                public Object loadObject() {
	                    return impls[0];
	                }
	            },
	            new Dispatcher() {
	                public Object loadObject() {
	                    return impls[1];
	                }
	            }
	        };

	        Enhancer e = new Enhancer();
	        e.setInterfaces(new Class[]{ Foo.class, Bar.class });
	        e.setCallbacks(callbacks);
	        e.setCallbackFilter(new CallbackFilter() {
	            public int accept(Method method) {
	                return (method.getDeclaringClass().equals(Foo.class)) ? 0 : 1;
	            }
	        });
	        return e.create();
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(((Foo)create()).foo().equals("foo1"));
	}

}

interface Foo {
	String foo();
}

interface Bar {
	String bar();
}